<?php
if(!defined('MIRAGE_MODE')) die('Forbidden');

require_once 'core/SystemHandler.php';
include_once 'core/BaseController.php';
include_once 'core/BaseModel.php';
include_once 'core/Html.php';
include_once 'core/SystemException.php';
include_once 'core/Router.php';
include_once 'defaultpage/ErrorController.php';
include_once 'util/CVarDumper.php';

if(MIRAGE_MODE == 'dev')
   include 'core/Checker.php';

/**
 * Description of Mirage
 * Date Created Aug 30, 2011 11:51:32 AM
 * @author Bryan Salazar
 */
class Mirage {
   protected static $_instance = null;
   protected $_config;
   protected $_config_file;
   public $_app_path;
   public $param;
   public $controller = '';
   public $method = '';
   
   protected function Mirage($application_path,$config_file) {
      SystemHandler::init();
      $this->_config = include_once $config_file;
      $this->_config_file = $config_file;
      $this->_app_path = $application_path;
   }
   
   public function getConfig() {
      return $this->_config;
   }
   
   public function getConfigFile() {
      return $this->_config_file;
   }
   
   public function getConfigDb($db = 'db') {
      if(!isset($this->_config[$db]))
         throw new SystemException('Key "' . $db . '" not exist in ' . $this->_config_file);        
      return $this->_config[$db];
   }
   
   public function getAppPath() {
      return $this->_app_path;
   }
   
   public static function app() {
      return self::$_instance;
   }
   
   public static function createWebApps($application_path,$config_file) {
      if(!self::$_instance)
         self::$_instance = new Mirage($application_path,$config_file);   
      return self::$_instance;
   }
   
   public function createUrl($string,$param=array()) {
      $domain = $this->_config['domain'];
      $parameters = '';
      if($param)
         $parameters = '/';
      foreach($param as $key => $value) {
         $parameters.= $key . '/' . $value;
      }
      $url = $domain . $parameters;
      return $url;
      
   }
   
   public function baseUrl() {
      $domain = str_replace('index.php', '', $this->_config['domain']);
      
      return $domain;
   }
   
   public function redirect($string,$param=array()) {
      $url = $this->createUrl($string, $param);
      header('LOCATION: http://' . $url);
   }
   
   public function run() {
      if(MIRAGE_MODE == 'dev') {
         Checker::checkConfigAndDirectory($this->_config,  $this->_config_file);
      }
      
      $this->param = $this->_config['param'];
      Router::route();
   }
   
   public function loadModel($model) {
      $file = self::app()->getAppPath() . '/models/' . $model . '.php';
      if(!file_exists($file)) {
         throw new SystemException('Model ' .$model .' does not exist ' .$file);
      }
      include $file;
   }
   
   public function loadLibrary($library) {
      $file = self::app()->getAppPath() . '/libraries/' . $library . '.php';
      if(!file_exists($file)) {
         throw new SystemException('Library ' .$library .' does not exist ' .$file);
      }
      include $file;
   }
   
   public function loadThirdParty($third_party) {
      $file = self::app()->getAppPath() . '/third_party/' . $third_party . '.php';
      if(!file_exists($file)) {
         throw new SystemException('Third Party ' .$third_party .' does not exist ' .$file);
      }
      include $file;
   }

   public function loadComponent($component) {
      $file = self::app()->getAppPath() . '/component/' . $component . '.php';
      if(!file_exists($file)) {
         throw new SystemException('Third Party ' .$component .' does not exist ' .$file);
      }
      include $file;
   }
}
